package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.ExperienceRuleDao;
import com.qdlc.p2p.dal.dto.ExperienceRule;

/**
 * 体验券规则DAO接口
 * @author xxx
 * @version 2.0
 * @since 2015年04月23日 10时50分33秒
 */
@Repository(value = "experienceRuleDao")
public class ExperienceRuleDaoImpl extends BaseDaoImpl<ExperienceRule> implements ExperienceRuleDao {

    @SuppressWarnings("unchecked")
    @Override
    public ExperienceRule getAdditionalExperienceRuleById(long id) {
        String jpql = "from AdditionalExperienceRule where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<ExperienceRule> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (ExperienceRule) list.get(0);
        } else {
            return null;
        }
    }

	@Override
	public int updateQuotaYes(long id) {
		String jpql = "UPDATE ExperienceRule set quotaYes = quotaYes+1 WHERE quotaYes+1 <= quota AND id = :id";
		return updateByJpql(jpql, new String[]{"id"}, new Object[]{id});
	}
    
    
}
